package com.ferryc.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ferryc.entity.Account;
import com.ferryc.entity.AccountDetail;
import com.ferryc.exception.NSFException;
import com.ferryc.mapper.AccountDetailMapper;
import com.ferryc.service.IAccountService;
import com.ferryc.mapper.AccountMapper;
import com.ferryc.utils.DigitalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * Created by Ferry Chen on 2019-11-06.
 */
@Service
@Slf4j
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements IAccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private AccountDetailMapper accountDetailMapper;

    @Override
    @Transactional
    public boolean save(Account account) {
        Account qa = new Account();
        qa.setUserId(account.getUserId());
        List<Account> list = accountMapper.queryList(qa);
        if (CollectionUtils.isEmpty(list)) {
            String id = UUID.randomUUID().toString();
            account.setId(id);
            saveDetail("1", account.getId(), account.getMoney());
            return super.save(account);
        }


        return false;
    }


    @Override
    @Transactional
    public void update(Account account) {
        Account qa = new Account();
        qa.setUserId(account.getUserId());
        double money = account.getMoney();
        List<Account> list = accountMapper.queryList(qa);
        if (!CollectionUtils.isEmpty(list)) {
            account.setId(list.get(0).getId());
            account.setMoney(DigitalUtils.add(account.getMoney(), list.get(0).getMoney()));
            super.saveOrUpdate(account);
            saveDetail("1", account.getId(), money);
        }
    }

    @Override
    @Transactional
    public void consume(String userId, double money) {
        Account qa = new Account();
        qa.setUserId(userId);
        List<Account> list = accountMapper.queryList(qa);
        if (!CollectionUtils.isEmpty(list)) {
            Account account = list.get(0);
            if (money > account.getMoney()) {
                throw new NSFException();
            }
            account.setMoney(DigitalUtils.subtract(account.getMoney(), money));
            account.setId(list.get(0).getId());
            super.saveOrUpdate(account);
            saveDetail("2", account.getId(), money);
        }
    }

    @Override
    public boolean checkBalance(String userId, double money) {
        Account qa = new Account();
        qa.setUserId(userId);
        List<Account> list = accountMapper.queryList(qa);
        if (!CollectionUtils.isEmpty(list)) {
            return money < list.get(0).getMoney();
        } else {
            return false;
        }
    }

    private void saveDetail(String type, String accountId, double money) {
        AccountDetail detail = new AccountDetail();
        detail.setAccountId(accountId);
        detail.setMoney(money);
        detail.setType(type);
        accountDetailMapper.insert(detail);
    }


}
